//
//  ReceiveTaskView.m
//  Safflower
//
//  Created by 袁亮 on 2016/12/23.
//  Copyright © 2016年 Migic_Z. All rights reserved.
//

#import "ReceiveTaskView.h"

@interface ReceiveTaskView()

@property (nonatomic, strong) UIView *leftView;
@property (nonatomic, strong) UIView *rightView;

@property (nonatomic, strong) UILabel *date_label;
@property (nonatomic, strong) UILabel *do_person_label;
@property (nonatomic, strong) UILabel *push_person_label;
@property (nonatomic, strong) UITextView *desc_text_view;
@property (nonatomic, strong) UITextView *word_text_view;

@property (nonatomic, strong) UIImageView *gift_image_view;

@property (nonatomic, strong) TaskModel *taskModel;

@end

@implementation ReceiveTaskView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.layer.masksToBounds = YES;
        self.layer.borderWidth = 1.0;
        self.layer.borderColor = [[UIColor blackColor] CGColor];
        self.layer.cornerRadius = 10.0f;
        
        [self makeLeftView];
        
        [self makeRightView];
        
    }
    return self;
}

- (void)makeLeftView
{
    _leftView = [UIView new];
    [self addSubview:_leftView];
    [_leftView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.mas_equalTo(0);
        make.width.mas_equalTo((SCREEN_WIDTH - 80) / 2);
    }];
    
    UILabel *dateLabel = [UILabel new];
    dateLabel.textColor = [UIColor blackColor];
    dateLabel.font = [UIFont systemFontOfSize:19.0f];
    dateLabel.text = @"周期";
    [_leftView addSubview:dateLabel];
    [dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.mas_equalTo(10);
        make.size.mas_equalTo(CGSizeMake(80, 20));
    }];
    
    UILabel *doPersonLabel = [UILabel new];
    doPersonLabel.textColor = [UIColor blackColor];
    doPersonLabel.font = [UIFont systemFontOfSize:19.0f];
    doPersonLabel.text = @"执行人";
    [_leftView addSubview:doPersonLabel];
    [doPersonLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(dateLabel.mas_bottom).mas_offset(10);
        make.left.mas_equalTo(10);
        make.size.mas_equalTo(CGSizeMake(80, 20));
    }];
    
    UILabel *pushPersonLabel = [UILabel new];
    pushPersonLabel.textColor = [UIColor blackColor];
    pushPersonLabel.font = [UIFont systemFontOfSize:19.0f];
    pushPersonLabel.text = @"兑奖人";
    [_leftView addSubview:pushPersonLabel];
    [pushPersonLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(doPersonLabel.mas_bottom).mas_offset(10);
        make.left.mas_equalTo(10);
        make.size.mas_equalTo(CGSizeMake(80, 20));
    }];
    
    _date_label = [UILabel new];
    _date_label.textColor = [UIColor blackColor];
    _date_label.font = [UIFont systemFontOfSize:17.0f];
    _date_label.text = @"一个月";
    [_leftView addSubview:_date_label];
    [_date_label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.mas_equalTo(dateLabel.mas_right).mas_offset(20);
        make.right.mas_equalTo(-10);
        make.height.mas_equalTo(20);
    }];
    
    _do_person_label = [UILabel new];
    _do_person_label.textColor = [UIColor blackColor];
    _do_person_label.font = [UIFont systemFontOfSize:17.0f];
    [_leftView addSubview:_do_person_label];
    [_do_person_label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_date_label.mas_bottom).mas_offset(10);
        make.left.mas_equalTo(doPersonLabel.mas_right).mas_offset(20);
        make.right.mas_equalTo(-10);
        make.height.mas_equalTo(20);
    }];
    
    _push_person_label = [UILabel new];
    _push_person_label.textColor = [UIColor blackColor];
    _push_person_label.font = [UIFont systemFontOfSize:17.0f];
    [_leftView addSubview:_push_person_label];
    [_push_person_label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_do_person_label.mas_bottom).mas_offset(10);
        make.left.mas_equalTo(pushPersonLabel.mas_right).mas_offset(20);
        make.right.mas_equalTo(-10);
        make.height.mas_equalTo(20);
    }];
    
    /// ↑↑↑↑↑↑↑  如上高度总共120
    CGFloat textHeight = (SCREEN_HEIGHT - 256) / 2;
    
    UILabel *taskDescLabel = [UILabel new];
    taskDescLabel.font = [UIFont systemFontOfSize:19.0f];
    taskDescLabel.text = @"任务描述";
    [_leftView addSubview:taskDescLabel];
    [taskDescLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(pushPersonLabel.mas_bottom).mas_offset(10);
        make.left.mas_equalTo(10);
        make.size.mas_equalTo(CGSizeMake(80, 20));
    }];
    
    _desc_text_view = [UITextView new];
    _desc_text_view.layer.masksToBounds = YES;
    _desc_text_view.layer.borderWidth = 1.0;
    _desc_text_view.layer.borderColor = [[UIColor blackColor] CGColor];
    _desc_text_view.layer.cornerRadius = 5.0f;
    _desc_text_view.font = [UIFont systemFontOfSize:17.0f];
    _desc_text_view.editable = NO;
    [_leftView addSubview:_desc_text_view];
    [_desc_text_view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(taskDescLabel.mas_bottom).mas_offset(10);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-10);
        make.height.mas_equalTo(textHeight);
    }];
    
    UILabel *teskWordLabel = [UILabel new];
    teskWordLabel.font = [UIFont systemFontOfSize:19.0f];
    teskWordLabel.text = @"任务寄语";
    [_leftView addSubview:teskWordLabel];
    [teskWordLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_desc_text_view.mas_bottom).mas_offset(10);
        make.left.mas_equalTo(10);
        make.size.mas_equalTo(CGSizeMake(80, 20));
    }];
    
    _word_text_view = [UITextView new];
    _word_text_view.layer.masksToBounds = YES;
    _word_text_view.layer.borderWidth = 1.0;
    _word_text_view.layer.borderColor = [[UIColor blackColor] CGColor];
    _word_text_view.layer.cornerRadius = 5.0f;
    _word_text_view.font = [UIFont systemFontOfSize:17.0f];
    _word_text_view.editable = NO;
    [_leftView addSubview:_word_text_view];
    [_word_text_view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(teskWordLabel.mas_bottom).mas_offset(10);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-10);
        make.height.mas_equalTo(textHeight);
    }];
    
}

- (void)makeRightView
{
    _rightView = [UIView new];
    [self addSubview:_rightView];
    [_rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.mas_equalTo(0);
        make.left.mas_equalTo(_leftView.mas_right).mas_offset(0);
    }];
    
    UILabel *giftLabel = [UILabel new];
    giftLabel.textColor = [UIColor blackColor];
    giftLabel.font = [UIFont systemFontOfSize:19.0f];
    giftLabel.text = @"奖励";
    [_rightView addSubview:giftLabel];
    [giftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.mas_equalTo(5);
        make.size.mas_equalTo(CGSizeMake(80, 20));
    }];
    
    CGFloat imageHeight = (SCREEN_HEIGHT - 76) / 2;
    
    _gift_image_view = [UIImageView new];
    _gift_image_view.layer.masksToBounds = YES;
    _gift_image_view.layer.borderWidth = 1.0;
    _gift_image_view.layer.borderColor = [[UIColor blackColor] CGColor];
    _gift_image_view.layer.cornerRadius = 5.0f;
    _gift_image_view.contentMode = UIViewContentModeScaleAspectFit;
    [_rightView addSubview:_gift_image_view];
    [_gift_image_view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(giftLabel.mas_bottom).mas_offset(10);
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.height.mas_equalTo(imageHeight);
    }];
    
    UIView *buttonView = [UIView new];
    [_rightView addSubview:buttonView];
    [buttonView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(_gift_image_view.mas_bottom).mas_offset(0);
        make.bottom.mas_equalTo(0);
    }];
    
    CGFloat buttonViewHeight = (SCREEN_HEIGHT - 76) - (imageHeight) - 40;
    
    UIButton *refuseButton = [UIButton new];
    [refuseButton setImage:[UIImage imageNamed:@"task_refuse_button"] forState:UIControlStateNormal];
    [refuseButton addTarget:self action:@selector(clickRefuseButton:) forControlEvents:UIControlEventTouchUpInside];
    [buttonView addSubview:refuseButton];
    [refuseButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(buttonViewHeight / 1.5, buttonViewHeight / 1.5));
        make.centerY.mas_equalTo(buttonView);
        make.left.mas_equalTo(30);
    }];
    
    UIButton *receiveButton = [UIButton new];
    [receiveButton setImage:[UIImage imageNamed:@"task_receive_button"] forState:UIControlStateNormal];
    [receiveButton addTarget:self action:@selector(clickReceiveButton:) forControlEvents:UIControlEventTouchUpInside];
    [buttonView addSubview:receiveButton];
    [receiveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(buttonViewHeight / 1.5, buttonViewHeight / 1.5));
        make.centerY.mas_equalTo(buttonView);
        make.right.mas_equalTo(-30);
    }];
}

- (void)loadReceiveTask:(TaskModel *)taskModel
{
    _taskModel = taskModel;

    _date_label.text = [NSString stringWithFormat:@"%@ 天",_taskModel.days];
    
    UserModel *user = [UserManager fetchUserInfo];
    _do_person_label.text = [NSString stringWithFormat:@"%@",user.username];
    
    _push_person_label.text = [NSString stringWithFormat:@"%@",_taskModel.friendInfo.username];
    
    _desc_text_view.text = _taskModel.descriptionText;
    
    _word_text_view.text = _taskModel.sendwordText;
    
    [_gift_image_view sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BaseIp,_taskModel.image]]];
}

- (void)clickRefuseButton:(UIButton *)button
{
    if ([self.delegate respondsToSelector:@selector(clickRefuseTask:)]) {
        [self.delegate clickRefuseTask:_taskModel];
    }
}

- (void)clickReceiveButton:(UIButton *)button
{
    if ([self.delegate respondsToSelector:@selector(clickReceiveTask:)]) {
        [self.delegate clickReceiveTask:_taskModel];
    }
}

@end
